package com.ibigcat.module.mdm.controller.admin.park;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.CommonResult;
import com.ibigcat.framework.common.util.object.BeanUtils;
import static com.ibigcat.framework.common.pojo.CommonResult.success;

import com.ibigcat.framework.excel.core.util.ExcelUtils;

import com.ibigcat.framework.apilog.core.annotation.ApiAccessLog;
import static com.ibigcat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ibigcat.module.mdm.controller.admin.park.vo.*;
import com.ibigcat.module.mdm.dao.entity.ParkDO;
import com.ibigcat.module.mdm.service.ParkService;

@Tag(name = "管理后台 - 园区管理")
@RestController
@RequestMapping("/mdm/park")
@Validated
public class ParkController {

    @Resource
    private ParkService parkService;

    @PostMapping("/create")
    @Operation(summary = "创建园区管理")
    @PreAuthorize("@ss.hasPermission('mdm:park:create')")
    public CommonResult<Long> createPark(@Valid @RequestBody ParkSaveReqVO createReqVO) {
        return success(parkService.createPark(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新园区管理")
    @PreAuthorize("@ss.hasPermission('mdm:park:update')")
    public CommonResult<Boolean> updatePark(@Valid @RequestBody ParkSaveReqVO updateReqVO) {
        parkService.updatePark(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除园区管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdm:park:delete')")
    public CommonResult<Boolean> deletePark(@RequestParam("id") Long id) {
        parkService.deletePark(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得园区管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdm:park:query')")
    public CommonResult<ParkRespVO> getPark(@RequestParam("id") Long id) {
        ParkDO park = parkService.getPark(id);
        return success(BeanUtils.toBean(park, ParkRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得园区管理分页")
    @PreAuthorize("@ss.hasPermission('mdm:park:query')")
    public CommonResult<PageResult<ParkRespVO>> getParkPage(@Valid ParkPageReqVO pageReqVO) {
        PageResult<ParkDO> pageResult = parkService.getParkPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ParkRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出园区管理 Excel")
    @PreAuthorize("@ss.hasPermission('mdm:park:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportParkExcel(@Valid ParkPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ParkDO> list = parkService.getParkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "园区管理.xls", "数据", ParkRespVO.class,
                        BeanUtils.toBean(list, ParkRespVO.class));
    }

}